---
output:
  bookdown::pdf_document2:
    fig_caption: yes
    keep_tex: yes
    template: header.tex
    toc: no
    citation_package: natbib
    latex_engine: xelatex
  html_document:
    df_print: paged
  pdf_document:
    toc: no
  word_document: default
bibliography: master.bib
geometry: margin=1in
keywords: 'Party Identity, Policy Vote, Party labels'
classoption: a4paper
biblio-style: apsr
spacing: double

---

```{r,include=FALSE}
library(knitr)
library(kableExtra)
knitr::opts_chunk$set(eval = TRUE, echo = FALSE, message=FALSE, warning=FALSE)
knitr::opts_chunk$set(
  tab.cap.style = "Table Caption",
  tab.cap.pre = "Table ",
  tab.cap.sep = ": "
  )

library(haven)
library(tidyverse)
library(naniar)
library(sjPlot)
library(ggthemes)
library(gridExtra)
library(scales)
Data_Exp1<-read.csv("Data_Exp08.csv")
Data_Exp1AB<-read.csv("Data_Exp0_AB8.csv")

Data_Exp1 <- Data_Exp1 %>%
  mutate(past_vote_2019_Respondent = fct_recode(as.factor(past_vote_2019_Respondent),
                                "Conservative" = "1",
                                       "Labour" = "2",
                             "Liberal Democrat" = "3",
                 "Scottish National Party (SNP)" = "4",
                                  "Plaid Cymru" = "5",
                                  "Green Party" = "7",
                                        "Other" = "9",
                                 "Brexit Party" = "12",
                     "An independent candidate" = "13"),
         Past_vote_2019_Lab_or_Con = scale(case_when(
               past_vote_2019_Respondent == "Conservative" ~ 1,
               past_vote_2019_Respondent == "Labour" ~ -1,
               TRUE ~ 0))
  )

Data_Exp1 <- Data_Exp1 %>%
  mutate(past_vote_2017_Respondent = fct_recode(as.factor(past_vote_2017_Respondent),
                                                "Conservative" = "1",
                                                "Labour" = "2",
                                                "Liberal Democrat" = "3",
                                                "Scottish National Party (SNP)" = "4",
                                                "Plaid Cymru" = "5",
                                                "Green Party" = "7",
                                                "Other" = "9",
                                                "Brexit Party" = "12",
                                                "An independent candidate" = "13"),
         Past_vote_2017_Lab_or_Con = case_when(
           past_vote_2017_Respondent == "Conservative" ~ 1,
           past_vote_2017_Respondent == "Labour" ~ -1,
           TRUE ~ 0)
  )

library(modelr)

Data_Exp1 <-   Data_Exp1 %>%
  mutate(
  euRefVote_Respondent = na_if(euRefVote_Respondent, 2)
  )%>%
  mutate(
    Sex_Closeness= abs(Sex_Lab_Profile-gender_Respondent) - abs(Sex_Con_Profile-gender_Respondent),
    Brexit_Closeness = abs(Brexit_Lab_Profile-euRefVote_Respondent) - abs(Brexit_Con_Profile-euRefVote_Respondent),
    LRD_Closeness =  abs(LR_Lab_Profile - leftRight_Respondent)- abs(LR_Con_Profile - leftRight_Respondent),
    BornConst_Closeness= BornConst_Con_Profile - BornConst_Lab_Profile,
    Age_Closeness =  abs(Age_Lab_Profile - age_respondent) - abs(Age_Con_Profile - age_respondent),
    CutsSpending_Closeness = abs(CutsSpending_Lab_Profile - cutsTooFar_Respondent) - abs(CutsSpending_Con_Profile-cutsTooFar_Respondent),
    Environment_Closeness = abs(Environment_Lab_Profile - enviroProtection_Respondent) -  abs(Environment_Con_Profile -enviroProtection_Respondent),
    Redistribution_Closeness = abs(Redistribution_Lab_Profile - redist_Respondent) - abs(Redistribution_Con_Profile - redist_Respondent),
    Immigration_Closeness = abs(ImmigrationEcon_Lab_Profile - immigEcon_Respondent) - abs(ImmigrationEcon_Con_Profile - immigEcon_Respondent),
    ExperienceCouncil_Closeness = ExperienceCouncil_Con_Profile - ExperienceCouncil_Lab_Profile
  )

Data_Exp1 <- Data_Exp1 %>%
  mutate(
    Closer_Con_Brexit = Brexit_Closeness > 0,
    Closer_Lab_Brexit = Brexit_Closeness < 0,
    Closer_Con_LR = LRD_Closeness>0,
    Closer_Lab_LR = LRD_Closeness<0,
    Closer_Con_CutsSpending = CutsSpending_Closeness>0,
    Closer_Lab_CutsSpending = CutsSpending_Closeness<0,
    Closer_Con_Immigration = Immigration_Closeness>0,
    Closer_Lab_Immigration = Immigration_Closeness<0,
    Closer_Con_Environment = Environment_Closeness>0,
    Closer_Lab_Environment = Environment_Closeness<0,
    Closer_Con_Redistribution = Redistribution_Closeness>0,
    Closer_Lab_Redistribution = Redistribution_Closeness<0
  )

Data_Exp1<- Data_Exp1 %>%
dplyr::mutate(
    Sex_profDif= Sex_Con_Profile-Sex_Lab_Profile,
    Brexit_profDif = Brexit_Con_Profile-Brexit_Lab_Profile,
    LRD_profDif =  LR_Con_Profile-LR_Lab_Profile,
    BornConst_profDif= BornConst_Con_Profile - BornConst_Lab_Profile,
    Age_profDif =  Age_Con_Profile-Age_Lab_Profile,
    CutsSpending_profDif = CutsSpending_Con_Profile-CutsSpending_Lab_Profile,
    Environment_profDif = Environment_Con_Profile-Environment_Lab_Profile,
    Redistribution_profDif = Redistribution_Con_Profile-Redistribution_Lab_Profile,
    Immigration_profDif = ImmigrationEcon_Con_Profile-ImmigrationEcon_Lab_Profile,
    ExperienceCouncil_profDif = ExperienceCouncil_Con_Profile - ExperienceCouncil_Lab_Profile
  )

## Same analysis for 2017


VoteCorrectByLabel <- Data_Exp1 %>%
  filter(past_vote_2017_Respondent %in% c("Conservative","Labour")) 
VoteCorrectByLabel <-VoteCorrectByLabel %>%
  mutate(qsplit = fct_recode(as.factor(qsplit),  "Party Label"="1",  "No Party Label" = "2")) %>%
  group_by(qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(chosen_CandidateN ~
                                         Sex_Closeness
                                       + Brexit_Closeness
                                       + scale(LRD_Closeness)
                                       + BornConst_Closeness
                                       + Age_Closeness
                                       + scale(CutsSpending_Closeness)
                                       + scale(Environment_Closeness)
                                       + scale(Redistribution_Closeness)
                                       + scale(Immigration_Closeness)
                                       + ExperienceCouncil_Closeness, data =., w =pull(., W8))),
                   #            results = map(fit, effectsize::standardize_parameters),
                   results = map(fit, broom::tidy)
  ) %>%
  unnest(results)

VoteCorrectByLabel <- VoteCorrectByLabel %>%
  mutate(
    term = fct_recode(as.factor(term),
                      "Sex"="Sex_Closeness","Age"="Age_Closeness" ,"Born in Constituency"="BornConst_Closeness","Brexit"="Brexit_Closeness" ,
                      "Experience in Council"= "ExperienceCouncil_Closeness","Left-Right"="scale(LRD_Closeness)","Spending Cuts" = "scale(CutsSpending_Closeness)","Environment"="scale(Environment_Closeness)",
                      "Immigration"="scale(Immigration_Closeness)","Redistribution"="scale(Redistribution_Closeness)"
    )
  )
P.Closeness2017 <- VoteCorrectByLabel %>%
  filter(!term == "(Intercept)" ) %>%
  mutate(
    term = fct_relevel(term,
                       "Sex","Age","Brexit","Born in Constituency",
                       "Experience in Council","Left-Right","Spending Cuts","Environment",
                       "Immigration","Redistribution")
  )%>%
  ggplot( aes(x = estimate, y = term, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
  geom_vline(xintercept=0, linetype="dashed", 
             size=0.5, color="blue", alpha=0.5)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim = c(-0.2,1.3))+
  theme_clean()

VoteCorrectByLabel2017 <- Data_Exp1 %>%
  filter(past_vote_2017_Respondent %in% c("Conservative","Labour")) %>%
  mutate(qsplit = fct_recode(as.factor(qsplit), "Party Label"="1", "No Party Label"="2")) %>%
  group_by(qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(chosen_CandidateN ~
                                         Sex_Closeness
                                       + Brexit_Closeness
                                       + scale(LRD_Closeness)
                                       + BornConst_Closeness
                                       + Age_Closeness
                                       + scale(CutsSpending_Closeness)
                                       + scale(Environment_Closeness)
                                       + scale(Redistribution_Closeness)
                                       + scale(Immigration_Closeness)
                                       + ExperienceCouncil_Closeness 
                                       + Past_vote_2017_Lab_or_Con, data =., w =pull(., W8))),
                   results = map(fit, broom::tidy)) %>%
  unnest(results)

VoteCorrectByLabel2017 <- VoteCorrectByLabel2017 %>%
  mutate(
    term = fct_recode(as.factor(term),
                      "Sex"="Sex_Closeness","Age"="Age_Closeness" ,"Born in Constituency"="BornConst_Closeness","Brexit"="Brexit_Closeness" ,
                      "Experience in Council"= "ExperienceCouncil_Closeness","Left-Right"="scale(LRD_Closeness)","Spending Cuts" = "scale(CutsSpending_Closeness)","Environment"="scale(Environment_Closeness)",
                      "Immigration"="scale(Immigration_Closeness)","Redistribution"="scale(Redistribution_Closeness)"
    )
  )

P.Closeness_Pastvote2017 <- VoteCorrectByLabel2017 %>%
  filter(!term == "(Intercept)" ) %>%
  mutate(
    term = fct_recode(as.factor(term),"Past Party Vote 2017" ="Past_vote_2017_Lab_or_Con"),
    term = fct_relevel(term, "Past Party Vote 2017",
                       "Sex","Age","Brexit","Born in Constituency",
                       "Experience in Council","Left-Right","Spending Cuts","Environment",
                       "Immigration","Redistribution")
  )%>%
  ggplot( aes(x = estimate, y = term, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5)+
  geom_vline(xintercept=0, linetype="dashed", 
             size=0.5, color="blue", alpha=0.5)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim = c(-0.2,1.3))+
  theme_clean()

#Measure fit with interaction

VoteCorrectByLabel_interactionAB <- Data_Exp1AB %>%
  filter(past_vote_2019_Respondent %in% c("Conservative","Labour")) %>%
  mutate(qsplit = fct_recode(as.factor(qsplit), "Party Label"="1", "No Party Label"="2")) %>%
  group_by(qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Candidate_ChosenN ~
                                         (Sex_Closeness
                                       + Brexit_Closeness
                                       + scale(LRD_Closeness)
                                       + BornConst_Closeness
                                       + Age_Closeness
                                       + scale(CutsSpending_Closeness)
                                       + scale(Environment_Closeness)
                                       + scale(Redistribution_Closeness)
                                       + scale(Immigration_Closeness)
                                       + ExperienceCouncil_Closeness)^2 
                                       + Party_Closeness, data =., w =pull(., W8))),
                   results = map(fit, broom::tidy)) %>%
  unnest(results)%>%
  select(-fit)

VoteCorrectByLabel_interactionAB <- VoteCorrectByLabel_interactionAB %>%
  mutate(
    term = fct_recode(as.factor(term),
                      "Sex"="Sex_Closeness","Age"="Age_Closeness" ,"Born in Constituency"="BornConst_Closeness","Brexit"="Brexit_Closeness" ,
                      "Experience in Council"= "ExperienceCouncil_Closeness","Left-Right"="scale(LRD_Closeness)","Spending Cuts" = "scale(CutsSpending_Closeness)","Environment"="scale(Environment_Closeness)",
                      "Immigration"="scale(Immigration_Closeness)","Redistribution"="scale(Redistribution_Closeness)"
    ),
    term = str_remove_all(as.character(term), "_Closeness"),
    term = str_remove_all(term, "scale"),
    term = str_remove_all(term, "[)]"),
        term = str_remove_all(term, "[()]"),
    term = str_replace_all(term, "LRD","Left-Right")
  )

P.Closeness_Pastvote2019_interactionAB <- VoteCorrectByLabel_interactionAB %>%
  filter(!term == "(Intercept)" ) %>%
  mutate(
    term = fct_recode(as.factor(term),"Matching Party Vote 2019" ="Party_Closeness"),
    term = fct_relevel(term, "Matching Party Vote 2019",
                       "Sex","Age","Brexit","Born in Constituency",
                       "Experience in Council","Left-Right","Spending Cuts","Environment",
                       "Immigration","Redistribution")
  )%>%
  ggplot( aes(x = estimate, y = term, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5)+
  geom_vline(xintercept=0, linetype="dashed", 
             size=0.5, color="blue", alpha=0.5)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim = c(-0.2,1.3))+
  theme_clean()


#Measure with quadratic relation (only quadratic council experience is significant)

VoteCorrectByLabel_quadraticAB <- Data_Exp1AB %>%
  filter(past_vote_2019_Respondent %in% c("Conservative","Labour")) %>%
  mutate(qsplit = fct_recode(as.factor(qsplit), "Party Label"="1", "No Party Label"="2")) %>%
  group_by(qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Candidate_ChosenN ~
                                         Sex_Closeness + I(Sex_Closeness^2)
                                       + Brexit_Closeness+I(Brexit_Closeness^2)
                                       + scale(LRD_Closeness)+I(scale(LRD_Closeness)^2)
                                       + BornConst_Closeness+I(BornConst_Closeness^2)
                                       + Age_Closeness+I(Age_Closeness^2)
                                       + scale(CutsSpending_Closeness)+I(scale(CutsSpending_Closeness)^2)
                                       + scale(Environment_Closeness)+I(scale(Environment_Closeness)^2)
                                       + scale(Redistribution_Closeness)+I(scale(Redistribution_Closeness)^2)
                                       + scale(Immigration_Closeness)+I(scale(Immigration_Closeness)^2)
                                       + ExperienceCouncil_Closeness+I(ExperienceCouncil_Closeness^2) 
                                       + Party_Closeness, data =., w =pull(., W8))),
                   results = map(fit, broom::tidy)) %>%
  unnest(results)%>%
  select(-fit)

VoteCorrectByLabel_quadraticAB <- VoteCorrectByLabel_quadraticAB %>%
  mutate(
    term = fct_recode(as.factor(term),
                      "Sex"="Sex_Closeness","Age"="Age_Closeness" ,"Born in Constituency"="BornConst_Closeness","Brexit"="Brexit_Closeness" ,
                      "Experience in Council"= "ExperienceCouncil_Closeness","Left-Right"="scale(LRD_Closeness)","Spending Cuts" = "scale(CutsSpending_Closeness)","Environment"="scale(Environment_Closeness)",
                      "Immigration"="scale(Immigration_Closeness)","Redistribution"="scale(Redistribution_Closeness)"
    ),
    term = str_remove_all(as.character(term), "_Closeness"),
    term = str_remove_all(term, "scale"),
    term = str_remove_all(term, "[)]"),
        term = str_remove_all(term, "[()]"),
    term = str_replace_all(term, "LRD","Left-Right")
  )

P.Closeness_Pastvote2019_quadraticAB <- VoteCorrectByLabel_quadraticAB %>%
  filter(!term == "(Intercept)" ) %>%
  mutate(
    term = fct_recode(as.factor(term),"Past Party Vote 2019" ="Past_vote_2019_Lab_or_Con"),
    term = fct_relevel(term, "Past Party Vote 2019",
                       "Sex","Age","Brexit","Born in Constituency",
                       "Experience in Council","Left-Right","Spending Cuts","Environment",
                       "Immigration","Redistribution")
  )%>%
  ggplot( aes(x = estimate, y = term, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5)+
  geom_vline(xintercept=0, linetype="dashed", 
             size=0.5, color="blue", alpha=0.5)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim = c(-0.2,1.3))+
  theme_clean()

## Same analysis for 2017

Data_Exp1 <- Data_Exp1 %>%
  mutate(past_vote_2017_Respondent = fct_recode(as.factor(past_vote_2017_Respondent),
                                                "Conservative" = "1",
                                                "Labour" = "2",
                                                "Liberal Democrat" = "3",
                                                "Scottish National Party (SNP)" = "4",
                                                "Plaid Cymru" = "5",
                                                "Green Party" = "7",
                                                "Other" = "9",
                                                "Brexit Party" = "12",
                                                "An independent candidate" = "13"),
         Past_vote_2017_Lab_or_Con = case_when(
           past_vote_2017_Respondent == "Conservative" ~ 1,
           past_vote_2017_Respondent == "Labour" ~ -1,
           TRUE ~ 0)
  )

ConVoteByPastAndLabel2017 <- Data_Exp1 %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    past_vote_2017_Respondent =
      fct_other(as.factor(past_vote_2017_Respondent),
                          keep = c("Conservative","Labour"),
                          other_level = 'Other')
                ) %>%
  mutate(Cons = chosen_CandidateN==1) %>%
  group_by(past_vote_2017_Respondent, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Cons ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) 

P.CorrectVoteCon <- ggplot(data = filter(ConVoteByPastAndLabel2017, !is.na(past_vote_2017_Respondent)) , aes(x = estimate, y = past_vote_2017_Respondent, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  theme_clean()

##Labour
LabVoteByPastAndLabel2017 <- Data_Exp1 %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    past_vote_2017_Respondent =
      fct_other(as.factor(past_vote_2017_Respondent),
                          keep = c("Conservative","Labour"),
                          other_level = 'Other')
                )   %>%
  mutate(Lab = chosen_CandidateN==-1) %>%
  group_by(past_vote_2017_Respondent, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Lab ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) 

P.CorrectVoteLab <- ggplot(data = filter(LabVoteByPastAndLabel2017, !is.na(past_vote_2017_Respondent)) , aes(x = estimate, y = past_vote_2017_Respondent, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  theme_clean()


#Unsure or neither

NoneVoteByPastAndLabel2017 <- Data_Exp1 %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    past_vote_2017_Respondent =
      fct_other(as.factor(past_vote_2017_Respondent),
                          keep = c("Conservative","Labour"),
                          other_level = 'Other')
                )  %>%
  mutate(None = chosen_CandidateN==0) %>%
  group_by(past_vote_2017_Respondent, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(None ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) 

#Combine
CorrectVote_Con2017<-ConVoteByPastAndLabel2017%>%
  dplyr::select(
    "past_vote2017"=past_vote_2017_Respondent, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "Conservative Candidate"
  )
CorrectVote_Lab2017<-LabVoteByPastAndLabel2017%>%
  dplyr::select(
    "past_vote2017"=past_vote_2017_Respondent, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "Labour Candidate"
  )

CorrectVote_None2017<-NoneVoteByPastAndLabel2017%>%
  dplyr::select(
    "past_vote2017"=past_vote_2017_Respondent, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "None"
  )

CorrectVote2017 <- rbind(CorrectVote_Con2017,CorrectVote_Lab2017,CorrectVote_None2017) %>%
  drop_na()

ggplot(CorrectVote2017, aes(x= Labels, y=Proportion, fill = Chosen_Candidates))+
  geom_col(position =position_stack())+
  facet_grid( ~past_vote2017)+
  scale_fill_manual(values= c("#0087DC", "#E4003B", "cornsilk4"))+
  theme_clean()
#+ theme(panel.margin = grid::unit(-1.25, "lines"))

CorrectVote2017<-CorrectVote2017%>%
  mutate(
    past_vote2017 =
      fct_recode(past_vote2017, "Conservative Voters" = "Conservative","Labour Voters" = "Labour")
  )%>%
  mutate(Chosen_Candidates = fct_recode(Chosen_Candidates, "Not sure or Not vote" = "None"),
         Chosen_Candidates = fct_relevel(Chosen_Candidates,"Not sure or Not vote","Labour Candidate","Conservative Candidate")
         )

P.CorrectVote2017 <- ggplot(CorrectVote2017 , aes(x = Proportion, y = Chosen_Candidates, col = Labels))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = Proportion-2*Std_Erro, xmax = Proportion+2*Std_Erro), width = 0.3) +
  facet_wrap(~past_vote2017)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim = c(0,1))+
  theme_clean()+
  theme(panel.background = element_rect(fill = NA, color = "black"))

```

```{r,include=FALSE}

library(papeR)

Respondents <- Data_Exp1%>%
  select(ends_with("_Respondent"))%>%
  select(-localTurnoutRetro_Respondent, -past_vote_2019_Respondent,-past_vote_2017_Respondent, -polAttention_Respondent)%>%
  rename(CutsSpending = cutsTooFar_Respondent, Brexit = euRefVote_Respondent, Environment = enviroProtection_Respondent, Immigration = immigEcon_Respondent, LR = leftRight_Respondent, Redistribution = redist_Respondent, Gender = gender_Respondent)%>%
  mutate(
    Brexit =dplyr::recode(as.factor(Brexit), "0"="Remain", "1"="Leave", "2"=NA_character_),
    Gender = dplyr::recode(as.factor(Gender), "1"="Male", "2" = "Female")
  )
  


Conservative <- Data_Exp1%>%
  select(ends_with("_Con_Profile"))%>%
  select(-ID_Con_Profile,-Party_Con_Profile)%>%
  rename(
    Gender ="Sex_Con_Profile",   Brexit ="Brexit_Con_Profile",   LR ="LR_Con_Profile",           
Borninconstituency = "BornConst_Con_Profile",  Age = "Age_Con_Profile",CutsSpending =  "CutsSpending_Con_Profile", Environment = "Environment_Con_Profile", Redistribution = "Redistribution_Con_Profile", Immigration = "ImmigrationEcon_Con_Profile",  
Council_experience= "ExperienceCouncil_Con_Profile"
  )%>%
  mutate(
    Brexit =dplyr::recode(as.factor(Brexit), "0"="Remain", "1"="Leave", "2"=NA_character_),
    Gender = dplyr::recode(as.factor(Gender), "0"="Male", "1" = "Female"),
    "Born in constituency" = dplyr::recode(as.factor(Borninconstituency), "0"="No", "1" = "Yes"),
    "Council experience" = dplyr::recode(as.factor(Council_experience), "0"="No", "1" = "Yes")
  )%>%
  select(-Borninconstituency)


Labour <- Data_Exp1%>%
  select(ends_with("_Lab_Profile"))%>%
  select(-ID_Lab_Profile,-Party_Lab_Profile)%>%
  rename(
    Gender ="Sex_Lab_Profile",   Brexit ="Brexit_Lab_Profile",   LR ="LR_Lab_Profile",           
Borninconstituency = "BornConst_Lab_Profile",  Age = "Age_Lab_Profile",CutsSpending =  "CutsSpending_Lab_Profile", Environment = "Environment_Lab_Profile", Redistribution = "Redistribution_Lab_Profile", Immigration = "ImmigrationEcon_Lab_Profile",  
Council_experience= "ExperienceCouncil_Lab_Profile"
  )%>%
  mutate(
    Brexit =dplyr::recode(as.factor(Brexit), "0"="Remain", "1"="Leave", "2"=NA_character_),
    Gender = dplyr::recode(as.factor(Gender), "0"="Male", "1" = "Female"),
    "Born in constituency" = dplyr::recode(as.factor(Borninconstituency), "0"="No", "1" = "Yes"),
    "Council experience" = dplyr::recode(as.factor(Council_experience), "0"="No", "1" = "Yes")
  )%>%
  select(-Borninconstituency)

#Separate model for Labour and Conservative

## Conservative

VoteLabel_heter_InteractABCons <-Data_Exp1AB %>%
    filter(past_vote_2019_Respondent %in% c("Conservative")) %>%
    mutate(qsplit = fct_recode(as.factor(qsplit), "Party Label"="1" , "No Party Label"="2")
    )%>%
    group_by(qsplit)%>%
    nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Candidate_ChosenN ~
                                  Sex_profDif*gender_Respondent
                                + Brexit_profDif*euRefVote_Respondent
                                + LRD_profDif*leftRight_Respondent
                                + BornConst_profDif
                                + Age_profDif*age_respondent
                                + CutsSpending_profDif*cutsTooFar_Respondent
                                + Environment_profDif*enviroProtection_Respondent
                                + Redistribution_profDif*redist_Respondent
                                + Immigration_profDif*immigEcon_Respondent
                                + ExperienceCouncil_profDif
                                + Party_profDif ,data =., w =pull(., W8)))
                   ,
                   results = map(fit, broom::tidy)) %>%
  unnest(results) %>%
  select(-fit)

VoteLabel_heter_InteractABCons <- VoteLabel_heter_InteractABCons %>%
  mutate(
    term = fct_recode(as.factor(term),
                      "Sex Candidate"="Sex_profDif","Age Candidate"="Age_profDif" ,"Born in Constituency Candidate"="BornConst_profDif","Brexit Candidate"="Brexit_profDif" ,
                      "Experience in Council Candidate"= "ExperienceCouncil_profDif","Left-Right Candidate"="LRD_profDif","Spending Cuts Candidate" = "CutsSpending_profDif","Environment Candidate"="Environment_profDif",
                      "Immigration Candidate"="Immigration_profDif","Redistribution Candidate"="Redistribution_profDif", "Age Interaction"="Age_profDif:age_respondent", "Brexit Interaction"= "Brexit_profDif:euRefVote_Respondent", "Spending Cuts Interaction"="CutsSpending_profDif:cutsTooFar_Respondent", "Immigration Interaction"="Immigration_profDif:immigEcon_Respondent" , "Environment Interaction"="Environment_profDif:enviroProtection_Respondent", "Left-Right Interaction"="LRD_profDif:leftRight_Respondent", "Redistribution Interaction"="Redistribution_profDif:redist_Respondent", "Sex Interaction"="Sex_profDif:gender_Respondent",
                      "Age Respondent"="age_respondent", "Brexit Respondent"= "euRefVote_Respondent", "Spending Cuts Respondent"="cutsTooFar_Respondent", "Immigration Respondent"="immigEcon_Respondent" , "Environment Respondent"="enviroProtection_Respondent", "Left-Right Respondent"="leftRight_Respondent", "Redistribution Respondent"="redist_Respondent", "Sex Respondent"="gender_Respondent" 
    )
  )


VoteLabel_heter_InteractABCons <- VoteLabel_heter_InteractABCons%>%
  filter(!term == "(Intercept)" ) %>%
  mutate(
    term = fct_recode(as.factor(term),"Conservative Candidate" ="Party_profDif"),
    term = fct_relevel(term, "Left-Right Respondent","Brexit Respondent","Spending Cuts Respondent","Redistribution Respondent","Immigration Respondent","Environment Respondent", "Sex Respondent","Age Respondent",
                       
"Left-Right Interaction","Brexit Interaction","Spending Cuts Interaction","Redistribution Interaction","Immigration Interaction","Environment Interaction", "Sex Interaction","Age Interaction",

"Conservative Candidate","Left-Right Candidate","Brexit Candidate","Spending Cuts Candidate","Redistribution Candidate","Immigration Candidate","Environment Candidate", "Sex Candidate","Age Candidate","Born in Constituency Candidate","Experience in Council Candidate")
  )%>%
  mutate(
    term = str_replace(term, " Candidate", "_Candidate"),
    term = str_replace(term, " Interaction", "_Interaction"),
    term = str_replace(term, " Respondent", "_Respondent")
  )%>%
  separate(term, into = c("term", "type"), sep="_")

P.FlexibleABCons<-VoteLabel_heter_InteractABCons%>%
  ggplot(aes(x = estimate, y = term, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
    geom_vline(xintercept=0, linetype="dashed", 
             size=0.5, color="blue", alpha=0.5)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim =c(-1.5,1.5))+
  facet_grid(type~., scales = "free_y")+
  theme_classic()+
  theme(legend.position="bottom", strip.text.y =element_text(angle = 0),
        panel.grid.major.y = element_line(colour = "grey95"),
        panel.background = element_rect(colour = "black"))+
  ggtitle("Conservative Voters")

## Labour
VoteLabel_heter_InteractABLab <-Data_Exp1AB %>%
    filter(past_vote_2019_Respondent %in% c("Labour")) %>%
    mutate(qsplit = fct_recode(as.factor(qsplit), "Party Label"="1" , "No Party Label"="2")
    )%>%
    group_by(qsplit)%>%
    nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Candidate_ChosenN ~
                                  Sex_profDif*gender_Respondent
                                + Brexit_profDif*euRefVote_Respondent
                                + LRD_profDif*leftRight_Respondent
                                + BornConst_profDif
                                + Age_profDif*age_respondent
                                + CutsSpending_profDif*cutsTooFar_Respondent
                                + Environment_profDif*enviroProtection_Respondent
                                + Redistribution_profDif*redist_Respondent
                                + Immigration_profDif*immigEcon_Respondent
                                + ExperienceCouncil_profDif
                                + Party_profDif ,data =., w =pull(., W8)))
                   ,
                   results = map(fit, broom::tidy)) %>%
  unnest(results) %>%
  select(-fit)

VoteLabel_heter_InteractABLab <- VoteLabel_heter_InteractABLab %>%
  mutate(
    term = fct_recode(as.factor(term),
                      "Sex Candidate"="Sex_profDif","Age Candidate"="Age_profDif" ,"Born in Constituency Candidate"="BornConst_profDif","Brexit Candidate"="Brexit_profDif" ,
                      "Experience in Council Candidate"= "ExperienceCouncil_profDif","Left-Right Candidate"="LRD_profDif","Spending Cuts Candidate" = "CutsSpending_profDif","Environment Candidate"="Environment_profDif",
                      "Immigration Candidate"="Immigration_profDif","Redistribution Candidate"="Redistribution_profDif", "Age Interaction"="Age_profDif:age_respondent", "Brexit Interaction"= "Brexit_profDif:euRefVote_Respondent", "Spending Cuts Interaction"="CutsSpending_profDif:cutsTooFar_Respondent", "Immigration Interaction"="Immigration_profDif:immigEcon_Respondent" , "Environment Interaction"="Environment_profDif:enviroProtection_Respondent", "Left-Right Interaction"="LRD_profDif:leftRight_Respondent", "Redistribution Interaction"="Redistribution_profDif:redist_Respondent", "Sex Interaction"="Sex_profDif:gender_Respondent",
                      "Age Respondent"="age_respondent", "Brexit Respondent"= "euRefVote_Respondent", "Spending Cuts Respondent"="cutsTooFar_Respondent", "Immigration Respondent"="immigEcon_Respondent" , "Environment Respondent"="enviroProtection_Respondent", "Left-Right Respondent"="leftRight_Respondent", "Redistribution Respondent"="redist_Respondent", "Sex Respondent"="gender_Respondent" 
    )
  )


VoteLabel_heter_InteractABLab <- VoteLabel_heter_InteractABLab%>%
  filter(!term == "(Intercept)" ) %>%
  mutate(
    term = fct_recode(as.factor(term),"Conservative Candidate" ="Party_profDif"),
    term = fct_relevel(term, "Left-Right Respondent","Brexit Respondent","Spending Cuts Respondent","Redistribution Respondent","Immigration Respondent","Environment Respondent", "Sex Respondent","Age Respondent",
                       
"Left-Right Interaction","Brexit Interaction","Spending Cuts Interaction","Redistribution Interaction","Immigration Interaction","Environment Interaction", "Sex Interaction","Age Interaction",

"Conservative Candidate","Left-Right Candidate","Brexit Candidate","Spending Cuts Candidate","Redistribution Candidate","Immigration Candidate","Environment Candidate", "Sex Candidate","Age Candidate","Born in Constituency Candidate","Experience in Council Candidate")
  )%>%
  mutate(
    term = str_replace(term, " Candidate", "_Candidate"),
    term = str_replace(term, " Interaction", "_Interaction"),
    term = str_replace(term, " Respondent", "_Respondent")
  )%>%
  separate(term, into = c("term", "type"), sep="_")

P.FlexibleABLab<-VoteLabel_heter_InteractABLab%>%
  ggplot(aes(x = estimate, y = term, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
    geom_vline(xintercept=0, linetype="dashed", 
             size=0.5, color="blue", alpha=0.5)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim =c(-1.5,1.5))+
  facet_grid(type~., scales = "free_y")+
  theme_classic()+
  theme(legend.position="bottom", strip.text.y =element_text(angle = 0),
        panel.grid.major.y = element_line(colour = "grey95"),
        panel.background = element_rect(colour = "black"))+
  ggtitle("Labour Voters")
```


```{r,include=FALSE}
## Analysis by levels of political attention

# For labour voters with high attention to politics party labels makes voters vote more aligned with past vote. Because we would suspect this voters would be better at guessing and more politically/ identity driven, this suggest for them the second element might be pushing their vote with party labels.

### High level of political attention

DataHAttnetion<-Data_Exp1 %>%
  filter(polAttention_Respondent>5)

ConVoteByPastAndLabelHA <- DataHAttnetion %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    past_vote_2019_Respondent =
      fct_other(as.factor(past_vote_2019_Respondent),
                          keep = c("Conservative","Labour"),
#         ,"Brexit Party"),
                          other_level = 'Other')
                ) %>%
  mutate(Cons = chosen_CandidateN==1) %>%
  group_by(past_vote_2019_Respondent, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Cons ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) %>%
  select(-fit)

P.CorrectVoteConHA <- ggplot(data = filter(ConVoteByPastAndLabelHA, !is.na(past_vote_2019_Respondent)) , aes(x = estimate, y = past_vote_2019_Respondent, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  theme_clean()

##Labour
LabVoteByPastAndLabelHA <- DataHAttnetion %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    past_vote_2019_Respondent =
      fct_other(as.factor(past_vote_2019_Respondent),
                          keep = c("Conservative","Labour"),
#         ,"Brexit Party"),
                          other_level = 'Other')
                )   %>%
  mutate(Lab = chosen_CandidateN==-1) %>%
  group_by(past_vote_2019_Respondent, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Lab ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) 

P.CorrectVoteLabHA <- ggplot(data = filter(LabVoteByPastAndLabelHA, !is.na(past_vote_2019_Respondent)) , aes(x = estimate, y = past_vote_2019_Respondent, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  theme_clean()


#Unsure or neither

NoneVoteByPastAndLabelHA <- DataHAttnetion %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    past_vote_2019_Respondent =
      fct_other(as.factor(past_vote_2019_Respondent),
                          keep = c("Conservative","Labour"),
#         ,"Brexit Party"),
                          other_level = 'Other')
                )  %>%
  mutate(None = chosen_CandidateN==0) %>%
  group_by(past_vote_2019_Respondent, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(None ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) 

#Combine
CorrectVote_ConHA<-ConVoteByPastAndLabelHA%>%
  dplyr::select(
    "past_vote2019"=past_vote_2019_Respondent, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "Conservative Candidate"
  )
CorrectVote_LabHA<-LabVoteByPastAndLabelHA%>%
  dplyr::select(
    "past_vote2019"=past_vote_2019_Respondent, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "Labour Candidate"
  )

CorrectVote_NoneHA<-NoneVoteByPastAndLabelHA%>%
  dplyr::select(
    "past_vote2019"=past_vote_2019_Respondent, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "None"
  )

CorrectVoteHA <- rbind(CorrectVote_ConHA,CorrectVote_LabHA,CorrectVote_NoneHA) %>%
  drop_na()

ggplot(CorrectVoteHA, aes(x= Labels, y=Proportion, fill = Chosen_Candidates))+
  geom_col(position =position_stack())+
  facet_grid( ~past_vote2019)+
  scale_fill_manual(values= c("#0087DC", "#E4003B", "cornsilk4"))+
  theme_clean()
#+ theme(panel.margin = grid::unit(-1.25, "lines"))

CorrectVoteHA<-CorrectVoteHA%>%
  mutate(
    past_vote2019 =
      fct_recode(past_vote2019, "Conservative Voters" = "Conservative","Labour Voters" = "Labour","Brexit Party Voters" = "Brexit Party")
  )%>%
  mutate(Chosen_Candidates = fct_recode(Chosen_Candidates, "Not sure or Not vote" = "None"),
         Chosen_Candidates = fct_relevel(Chosen_Candidates,"Not sure or Not vote","Labour Candidate","Conservative Candidate")
         )

P.CorrectVote2HA <- ggplot(CorrectVoteHA , aes(x = Proportion, y = Chosen_Candidates, col = Labels))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = Proportion-2*Std_Erro, xmax = Proportion+2*Std_Erro), width = 0.3) +
  facet_wrap(~past_vote2019)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim = c(0,1))+
  theme_clean()+
  theme(panel.background = element_rect(fill = NA, color = "black"))


### Low attention of political attention

DataLAttention<-Data_Exp1 %>%
  filter(polAttention_Respondent<5)

ConVoteByPastAndLabelLA <- DataLAttention %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    past_vote_2019_Respondent =
      fct_other(as.factor(past_vote_2019_Respondent),
                          keep = c("Conservative","Labour"),
#         ,"Brexit Party"),
                          other_level = 'Other')
                ) %>%
  mutate(Cons = chosen_CandidateN==1) %>%
  group_by(past_vote_2019_Respondent, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Cons ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) %>%
  select(-fit)

P.CorrectVoteConLA <- ggplot(data = filter(ConVoteByPastAndLabelLA, !is.na(past_vote_2019_Respondent)) , aes(x = estimate, y = past_vote_2019_Respondent, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  theme_clean()

##Labour
LabVoteByPastAndLabelLA <- DataLAttention %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    past_vote_2019_Respondent =
      fct_other(as.factor(past_vote_2019_Respondent),
                          keep = c("Conservative","Labour"),
#         ,"Brexit Party"),
                          other_level = 'Other')
                )   %>%
  mutate(Lab = chosen_CandidateN==-1) %>%
  group_by(past_vote_2019_Respondent, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Lab ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) 

P.CorrectVoteLabLA <- ggplot(data = filter(LabVoteByPastAndLabelLA, !is.na(past_vote_2019_Respondent)) , aes(x = estimate, y = past_vote_2019_Respondent, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  theme_clean()


#Unsure or neither

NoneVoteByPastAndLabelLA <- DataLAttention %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    past_vote_2019_Respondent =
      fct_other(as.factor(past_vote_2019_Respondent),
                          keep = c("Conservative","Labour"),
#         ,"Brexit Party"),
                          other_level = 'Other')
                )  %>%
  mutate(None = chosen_CandidateN==0) %>%
  group_by(past_vote_2019_Respondent, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(None ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) 

#Combine
CorrectVote_ConLA<-ConVoteByPastAndLabelLA%>%
  dplyr::select(
    "past_vote2019"=past_vote_2019_Respondent, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "Conservative Candidate"
  )
CorrectVote_LabLA<-LabVoteByPastAndLabelLA%>%
  dplyr::select(
    "past_vote2019"=past_vote_2019_Respondent, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "Labour Candidate"
  )

CorrectVote_NoneLA<-NoneVoteByPastAndLabelLA%>%
  dplyr::select(
    "past_vote2019"=past_vote_2019_Respondent, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "None"
  )

CorrectVoteLA <- rbind(CorrectVote_ConLA,CorrectVote_LabLA,CorrectVote_NoneLA) %>%
  drop_na()

ggplot(CorrectVoteLA, aes(x= Labels, y=Proportion, fill = Chosen_Candidates))+
  geom_col(position =position_stack())+
  facet_grid( ~past_vote2019)+
  scale_fill_manual(values= c("#0087DC", "#E4003B", "cornsilk4"))+
  theme_clean()
#+ theme(panel.margin = grid::unit(-1.25, "lines"))

CorrectVoteLA<-CorrectVoteLA%>%
  mutate(
    past_vote2019 =
      fct_recode(past_vote2019, "Conservative Voters" = "Conservative","Labour Voters" = "Labour")
#                 ,"Brexit Party Voters" = "Brexit Party")
  )%>%
  mutate(Chosen_Candidates = fct_recode(Chosen_Candidates, "Not sure or Not vote" = "None"),
         Chosen_Candidates = fct_relevel(Chosen_Candidates,"Not sure or Not vote","Labour Candidate","Conservative Candidate")
         )

P.CorrectVote2LA <- ggplot(CorrectVoteLA , aes(x = Proportion, y = Chosen_Candidates, col = Labels))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = Proportion-2*Std_Erro, xmax = Proportion+2*Std_Erro), width = 0.3) +
  facet_wrap(~past_vote2019)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim = c(0,1))+
  theme_clean()+
  theme(panel.background = element_rect(fill = NA, color = "black"))

 table(DataLAttention$past_vote_2019_Respondent, DataLAttention$chosen_CandidateN)

```

```{r,include=FALSE}
# Analysis by party ID

#Similar conclusions to analysis with level of attention. It seems voters with labour identity are more sensitive to party labels. This could mean that the lack of observed effect of labels in past labour voters comes from labour voters not identifying as labour.

DataPartyID<-Data_Exp1 %>%
  mutate(
    partyId = fct_recode(as.factor(partyId),
                         "Conservative"= "1",
                         "Labour" ="2",
                         "None" = "10")
  )

ConVoteByPartyIDAndLabel <- DataPartyID %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    partyId =
      fct_other(as.factor(partyId),
                          keep = c("1"= "Conservative",
                         "2"= "Labour",
                         "10"= "None"),
#         ,"Brexit Party"),
                          other_level = 'Other')
                ) %>%
  mutate(Cons = chosen_CandidateN==1) %>%
  group_by(partyId, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Cons ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) %>%
  select(-fit)

P.CorrectVoteConPartyID <- ggplot(data = filter(ConVoteByPartyIDAndLabel, !is.na(partyId)) , aes(x = estimate, y = partyId, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  theme_clean()

##Labour
LabVoteByPartyIDAndLabel <- DataPartyID %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    partyId =
      fct_other(as.factor(partyId),
                          keep = c("1"= "Conservative",
                         "2"= "Labour",
                         "10"= "None"),
#         ,"Brexit Party"),
                          other_level = 'Other')
                )   %>%
  mutate(Lab = chosen_CandidateN==-1) %>%
  group_by(partyId, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(Lab ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) 

P.CorrectVoteLabPartyID <- ggplot(data = filter(LabVoteByPartyIDAndLabel, !is.na(partyId)) , aes(x = estimate, y = partyId, col = qsplit))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = estimate-2*std.error, xmax = estimate+2*std.error), width = 0.5) +
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  theme_clean()


#Unsure or neither

NoneVoteByPartyIDAndLabel <- DataPartyID %>%
  mutate(
    qsplit = fct_recode(as.factor(qsplit), "Party Label" ="1", "No Party Label"="2"),
    partyId =
      fct_other(as.factor(partyId),
                          keep = c("1"= "Conservative",
                         "2"= "Labour",
                         "10"= "None"),
#         ,"Brexit Party"),
                          other_level = 'Other')
                )  %>%
  mutate(None = chosen_CandidateN==0) %>%
  group_by(partyId, qsplit)%>%
  nest() %>%
  dplyr::summarise(fit = map(data, ~lm(None ~ 1, data =., w =pull(., W8))),
            results = map(fit, broom::tidy)) %>%
  unnest(results) 

#Combine
CorrectVote_ConPartyID<-ConVoteByPartyIDAndLabel%>%
  dplyr::select(
    "partyId2019"=partyId, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "Conservative Candidate"
  )
CorrectVote_LabPartyID<-LabVoteByPartyIDAndLabel%>%
  dplyr::select(
    "partyId2019"=partyId, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "Labour Candidate"
  )

CorrectVote_NonePartyID<-NoneVoteByPartyIDAndLabel%>%
  dplyr::select(
    "partyId2019"=partyId, "Labels" = qsplit, "Proportion" = estimate, "Std_Erro"=std.error
  )%>%
  mutate(
    Chosen_Candidates = "None"
  )

CorrectVotePartyID <- rbind(CorrectVote_ConPartyID,CorrectVote_LabPartyID,CorrectVote_NonePartyID) %>%
  drop_na()

ggplot(CorrectVotePartyID, aes(x= Labels, y=Proportion, fill = Chosen_Candidates))+
  geom_col(position =position_stack())+
  facet_grid( ~partyId2019)+
  scale_fill_manual(values= c("#0087DC", "#E4003B", "cornsilk4"))+
  theme_clean()
#+ theme(panel.margin = grid::unit(-1.25, "lines"))

CorrectVotePartyID<-CorrectVotePartyID%>%
  mutate(
    PartyID2019 =
      fct_recode(partyId2019, "Conservative Identifier" = "Conservative","Labour Identifier" = "Labour")
#                 ,"Brexit Party Voters" = "Brexit Party")
  )%>%
  mutate(Chosen_Candidates = fct_recode(Chosen_Candidates, "Not sure or Not vote" = "None"),
         Chosen_Candidates = fct_relevel(Chosen_Candidates,"Not sure or Not vote","Labour Candidate","Conservative Candidate")
         )

P.CorrectVote2PartyID <- ggplot(CorrectVotePartyID , aes(x = Proportion, y = Chosen_Candidates, col = Labels))+
  geom_point(position = position_dodge(width =0.3))+
  geom_errorbar(position = position_dodge(width =0.3),
                aes(xmin = Proportion-2*Std_Erro, xmax = Proportion+2*Std_Erro), width = 0.3) +
  facet_wrap(~PartyID2019)+
  scale_color_manual(values= c("chartreuse3", "darkorchid3"), name = "")+
  labs(x="",y="")+
  coord_cartesian(xlim = c(0,1))+
  theme_clean()+
  theme(panel.background = element_rect(fill = NA, color = "black"))

```

## Appendix: Additional figures and tables

```{r Closeness2017, fig.width=9,fig.height=8,out.width="\\linewidth", echo=FALSE, fig.cap="Closeness of candidates' positions to respondents' positions with and without party labels as predictors of candidate choice.\nControlling for matching party affiliation (bottom) and not controlling for matching party affiliation (top). Note: only Conservative and Labour voters in the 2017 general election included.",fig.show='hold',fig.align='center', fig.pos="H"}
grid.arrange(P.Closeness2017,P.Closeness_Pastvote2017)
```

```{r Homophily-with-Interaction, fig.width=9,fig.height=8,out.width="\\linewidth", echo=FALSE, fig.cap="Closness as predictor of vote choice with first-degree interactions",fig.show='hold',fig.align='center', fig.pos="H"}

P.Closeness_Pastvote2019_interactionAB

```

```{r Homophily-with-Quadratic, fig.width=9,fig.height=8,out.width="\\linewidth", echo=FALSE, fig.cap="Closness as predictor of vote choice with quadratic model",fig.show='hold',fig.align='center', fig.pos="H"}

P.Closeness_Pastvote2019_quadraticAB

```



```{r Correctvote2017, fig.width=9,fig.height=8,out.width="\\linewidth", echo=FALSE, fig.cap="Proportion of respondents that chose each candidate in the experiment by their vote in the 2017 general election",fig.show='hold',fig.align='center', fig.pos="H"}
P.CorrectVote2017  

```


```{r Inference-and-Homophily, fig.width=12,fig.height=15,out.width="\\linewidth", echo=FALSE, fig.cap="Candidates' characteristics and interaction with respondents' characteristics as predictors of candidate choice" ,fig.show='hold',fig.align='center', fig.pos="H"}
grid.arrange(P.FlexibleABCons,P.FlexibleABLab)

```

```{r By-LevelofAttention, fig.width=12,fig.height=15,out.width="\\linewidth", echo=FALSE, fig.cap="Experiment choice by previous vote for respondents with low attention to politics (top) and high attention to politics (bottom)" ,fig.show='hold',fig.align='center', fig.pos="H"}
grid.arrange(P.CorrectVote2LA,P.CorrectVote2HA)

```

\newpage

```{r By-partyID, fig.width=12,fig.height=15,out.width="\\linewidth", echo=FALSE, fig.cap="Experiment choice by respondents party identification" ,fig.show='hold',fig.align='center', fig.pos="H"}

P.CorrectVote2PartyID
```


\newpage

```{r LevelsChunk4, echo=FALSE,out.width="98%", results='asis'}

options(knitr.kable.NA = '')

  kable(read.csv("Levels_Exp3.csv", fileEncoding = "UTF-8-BOM"), format="latex", caption = "\\label{tab:Levels}Levels used for each characteristic of candidates: first part",  booktabs = T, escape = TRUE) %>%
    kable_styling(full_width = F, font_size = 11,latex_options = c("striped", "hold_position")) %>%
column_spec(1, bold = T, width = "20em") %>%
column_spec(2, width = "24em") 


```

\newpage

```{r LevelsChunk3, echo=FALSE,out.width="98%", results='asis'}

options(knitr.kable.NA = '')

  kable(read.csv("Levels_Exp3a.csv", fileEncoding = "UTF-8-BOM"), format="latex", caption = "\\label{tab:Levels2}Levels used for each characteristic of candidates: second part",  booktabs = T, escape = TRUE) %>%
    kable_styling(full_width = F, font_size = 11,latex_options = c("striped", "hold_position")) %>%
column_spec(1, bold = T, width = "22em") %>%
column_spec(2, width = "20em") 


```


